package com.encryption.controls.util
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.core.UIComponent;
	import mx.events.FlexMouseEvent;
	import mx.events.SandboxMouseEvent;
	import mx.managers.PopUpManager;

	/**
	 * 鼠标在控件外面点击的处理帮助类
	 * 默认针对需要鼠标在外面点击就消失的控件
	 * @author jill
	 * 
	 */
	public class MouseDownOutHandleUtil
	{
		public function MouseDownOutHandleUtil()
		{
		}
		
		/**
		 * 增加鼠标在外面点击的事件
		 * @param target 要监听的控件对象
		 * @param handle 处理函数
		 * 
		 */
		public static function addListener(target:UIComponent, handle:Function=null):void{
			if(handle == null)
				handle = mouseOutsideHandler;
			target.addEventListener(FlexMouseEvent.MOUSE_DOWN_OUTSIDE, handle);
			target.addEventListener(FlexMouseEvent.MOUSE_WHEEL_OUTSIDE, handle);
			target.addEventListener(SandboxMouseEvent.MOUSE_DOWN_SOMEWHERE, handle);
			target.addEventListener(SandboxMouseEvent.MOUSE_WHEEL_SOMEWHERE, handle);
		}
		
		private static function mouseOutsideHandler(event:Event):void
		{
			var target:UIComponent = event.currentTarget as UIComponent;
			if (event is MouseEvent)
			{
				var mouseEvent:MouseEvent = MouseEvent(event);
				if (mouseEvent.target != target)
					return;
				
				if (!target.hitTestPoint(mouseEvent.stageX, mouseEvent.stageY, true))
				{
					PopUpManager.removePopUp(target);
				}
			}
			else if (event is SandboxMouseEvent) 
			{
				PopUpManager.removePopUp(target);
			}
		}
	}
}